package com.xnx3.j2ee.shiro.third.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xnx3.j2ee.func.HttpRequest;
import com.xnx3.j2ee.shiro.third.config.UserConfiguration;
import com.xnx3.j2ee.shiro.third.entity.AppUser;
import com.xnx3.j2ee.shiro.third.service.AppUserService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;

/**
 * @author xiaolu
 * @date 2018/7/2 上午10:03
 */
@Service
public class AppUserServiceImpl implements AppUserService {

    @Resource
    private UserConfiguration configuration;

    @Override
    public AppUser login(String token) {
        AppUser user = null;
        JSONObject param = new JSONObject();
        param.put("token", token);
        JSONObject result = HttpRequest.sentHttpRequest(configuration.getLoginUrl(), RequestMethod.POST, param);
        if (result != null && result.getInteger("code") == 200) {
            user = JSON.toJavaObject(result.getJSONObject("data").getJSONObject("user"), AppUser.class);
        }
        return user;
    }

    @Override
    public JSONObject register(AppUser user) {
        return HttpRequest.sentHttpRequest(configuration.getRegisterUrl(), RequestMethod.POST, (JSONObject) JSON.toJSON(user));
    }

}
